/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fallen.deck.front_end.jCustomTabbed;

import fallen.deck.entity.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class CustomTableModel extends DefaultTableModel {

    private String[] columnNames;
    protected int sortCol = 0;
    protected boolean isSortAsc = false;
    protected int m_result = 0;
    protected int columnsCount = 1;

    public CustomTableModel() {
        columnNames = new String[]{"Expansion", "Senda", "Tipo", "Carta",
            "Coste", "Frecuencia", "Cant."};
        columnsCount = columnNames.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    public String getColumnName(int column) {
        String str = columnNames[column];
        if (column == sortCol) {
            str += isSortAsc ? " >>" : " <<";
        }
        return str;
    }

    class ColumnListener extends MouseAdapter {

        protected JTable table;

        public ColumnListener(JTable t) {
            table = t;
        }

        public void mouseClicked(MouseEvent e) {
            TableColumnModel colModel = table.getColumnModel();
            int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
            int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();

            if (modelIndex < 0) {
                return;
            }

            if (sortCol == modelIndex) {
                isSortAsc = !isSortAsc;
            } else {
                sortCol = modelIndex;
            }

            for (int i = 0; i < columnsCount; i++) {
                TableColumn column = colModel.getColumn(i);
                column.setHeaderValue(getColumnName(i));
            }
            table.getTableHeader().repaint();

            Collections.sort(((DefaultTableModel) table.getModel()).getDataVector(), new CustomTableComparator(isSortAsc, modelIndex));
            table.tableChanged(new TableModelEvent(table.getModel()));
            table.repaint();
        }
    }

    class CustomTableComparator implements Comparator {

        protected boolean isSortAsc;
        protected int modelIndex;

        public CustomTableComparator(boolean sortAsc, int modelIndex) {
            isSortAsc = sortAsc;
            this.modelIndex = modelIndex;
        }

        public int compare(Object o1, Object o2) {
            int result = 0;
            Vector v1 = (Vector) o1;
            Vector v2 = (Vector) o2;
            switch (modelIndex) {
                case 0: {
                    result = ((Expansion) v1.get(modelIndex)).compareTo(v2.get(modelIndex));
                    break;
                }
                case 1: {
                    result = ((Pathway) v1.get(modelIndex)).compareTo(v2.get(modelIndex));
                    break;
                }
                case 2: {
                    result = ((Type) v1.get(modelIndex)).compareTo(v2.get(modelIndex));
                    break;
                }
                case 3: {
                    result = ((Card) v1.get(modelIndex)).compareTo(v2.get(modelIndex));
                    break;
                }
                case 4: {
                    result = ((String) v1.get(modelIndex)).compareTo((String) v2.get(modelIndex));
                    break;
                }
                case 5: {
                    result = ((Frequency) v1.get(modelIndex)).compareTo(v2.get(modelIndex));
                    break;
                }
                case 6: {
                    result = ((Integer) v1.get(modelIndex)) - ((Integer) v2.get(modelIndex));
                    break;
                }
            }

            if (!isSortAsc) {
                result = -result;
            }
            return result;
        }

        public boolean equals(Object obj) {
            if (obj instanceof CustomTableComparator) {
                CustomTableComparator compObj = (CustomTableComparator) obj;
                return compObj.isSortAsc == isSortAsc;
            }
            return false;
        }
    }
}
